import Vue from 'vue';
import Request from "@/sdk/pocky-request/index.js";
import TIM from 'tim-wx-sdk';
import {
	genTestUserSig
} from '@/sdk/timWx/GenerateTestUserSig.js';
var vm=new Vue();
const timStore = {
	state: {
		isReady:false,		//SDKready
		isLogin: '', //已登录的IMID
		timUserInfo:{},		//用户信息
		cvList:[],		//会话列表
	},
	mutations: {
		isReady: (state, payload) => {
			state.isReady = payload
		},
		isLogin: (state, payload) => {
			state.isLogin = payload
		},
		timUserInfo: (state, payload) => {
			state.timUserInfo = payload
		},
		cvList: (state, payload) => {
			state.cvList = state.cvList.concat(payload);
		},
	},
	getters: {
		
	},
	actions: {
		/**
		 * tim 用户登录
		 * @param {Object} context
		 * @param {Object} _imid
		 */
		timLogin(context,_imid){
			return new Promise(resolve => {
				let sigUser = genTestUserSig(_imid); 	
				vm.$tim.login({
					userID: _imid,
					userSig: sigUser.userSig
				}).then(res => {
					context.commit("isLogin",_imid);
					resolve(res)
				})
			})
		},
		/**
		 * @description TIM退出登录
		 * @param {Object} _this
		 */
		timLogOut(context){
			return new Promise(resolve => {
				vm.$tim.logout().then(res => {
					context.commit("isLogin",'');
					resolve(res)
				})
			})
		},
		/**TIM更新用户资料
		 * @param {Object} context
		 * @param {Object} _data
		 * nick 昵称 string
		 * avatar 头像地址 string
		 * gender 性别 string
		 * allowType 添加好友是否需要验证  string
		 * messageSettings 消息设置   0接收消息 ,1不接收消息
		 */
		timEditInfo(context,_data){
			return new Promise(resolve => {
				vm.$tim.updateMyProfile(_data).then(res => {
					resolve(res)
				})
			})
		},
		/**获取个人用户资料
		 * @param {Object} context
		 */
		timGetMyInfo(context){
			return new Promise(resolve => {
				vm.$tim.getMyProfile().then(res => {
					resolve(res)
				})
			})
		},
		/**
		 * 获取用户资料
		 * @param {Object} context
		 * @param {Object} _arr
		 * _arr 为要查询的用户ID数组  ['user0','user1']
		 */
		timGetUserInfo(context, _arr) {
			return new Promise((sucess, fail) => {
				vm.$tim.getUserProfile({
					userIDList: _arr
				}).then(res => {
					context.commit("timUserInfo",res);
					sucess(res)
				}).catch(err => {
					fail(err)
				})
			})
		},
		/**
		 * tim获取会话列表
		 * @param {Object} context
		 */
		timGetCovList(context){
			return new Promise(resolve=>{
				vm.$tim.getConversationList().then(res=>{
					resolve(res.data.conversationList)
				})
			})
		},
		/**
		 * 获取消息列表
		 * @param {Object} context
		 * @param {Object} _data
		 * conversationID 会话ID  string
		 * nextReqMessageID 分页  第一次拉取可不传  number
		 * count 每次拉取多少条,最大为15  number
		 */
		timGetMsgList(context,_data){
			return new Promise(resolve => {
				vm.$tim.getMessageList(_data).then(res => {
					resolve(res)
				})
			})
		},
		/**发送文本消息
		 * @param {Object} context
		 * @param {Object} _data
		 * data.to  目标用户ID
		 * data.payload 消息载体
		 * payload:{text:'文本内容'}
		 */
		timSendText(context,_data){
			console.log(_data);
			return new Promise((resolve, reject) => {
				var msg = vm.$tim.createTextMessage({
					to: _data.to,
					conversationType: TIM.TYPES.CONV_C2C,
					payload: _data.payload
				})
				
				vm.$tim.sendMessage(msg).then(res => {
					resolve(res)
				}).catch(err => {
					console.log(err);
					reject(err)
				})
			})
		},
		/**发送图片消息
		 * @param {Object} context
		 * @param {Object} _data
		 * data.to  目标用户ID
		 * data.payload 消息载体
		 * payload:{file:'图片地址'}
		 */
		timSendImg(context,_data){
			return new Promise((sucess, fail) => {
				let msg = vm.$tim.createImageMessage({
					to: _data.to,
					conversationType: TIM.TYPES.CONV_C2C,
					payload: _data.payload
				});
				
				vm.$tim.sendMessage(msg).then(res => {
					sucess(res)
				}).catch(err => {
					fail(err)
				})
			})
		},
		/**发送自定义消息
		 * @param {Object} context
		 * @param {Object} _data
		 * payload 字段
		 * data  order/reminder/good
		 * description	商品ID或订单orderNo
		 * extension
		 */
		timSendZdMsg(context,_data){
			return new Promise((sucess, fail) => {
				let msg = vm.$tim.createCustomMessage({
					to: _data.to,
					conversationType: TIM.TYPES.CONV_C2C,
					payload: _data.payload,
				});
				vm.$tim.sendMessage(msg).then(res => {
					sucess(res)
				}).catch(err => {
					fail(err)
				})
			})
		}
		
		
	}
}

export default timStore
